/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.jndi_config;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.naming.NamingException;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.StringRefAddr;

/**
 * Implementation of {@link StringContainer} that cam be used through JNDI.
 */
public class StringContainerImpl
    implements Referenceable, Serializable, StringContainer
{

  /**
   * the uid for serialization.
   */
  private static final long serialVersionUID = 4181993644720662855L;

  /**
   * the {@link Map} of {@link String} values.
   */
  private Map<String, String> values = new HashMap<String, String>();



  /**
   * constructor.
   */
  public StringContainerImpl()
  {
    // empty constructor.
  }



  /**
   * constructor.
   * 
   * @param reference
   *          the {@link Reference} that generate the creation of this
   *          {@link StringContainer}.
   */
  StringContainerImpl(Reference reference)
  {
    Enumeration<RefAddr> refAddrEnumeration = reference.getAll();
    while (refAddrEnumeration.hasMoreElements()) {
      RefAddr refAddr = refAddrEnumeration.nextElement();
      if (String.class.equals(refAddr.getContent().getClass())) {
        values.put(refAddr.getType(), (String) refAddr.getContent());
      }
    }
  }



  /**
   * Retrieves the Reference of this object.
   * 
   * @return The non-null Reference of this object.
   * @exception NamingException
   *              If a naming exception was encountered while retrieving the
   *              reference.
   * @see javax.naming.Referenceable#getReference()
   */
  public Reference getReference()
      throws NamingException
  {
    Reference ref = new Reference(StringContainerImpl.class.getName(), StringContainerFactory.class.getName(), null);
    for (Entry<String, String> valuesEntry : values.entrySet()) {
      ref.add(new StringRefAddr(valuesEntry.getKey(), valuesEntry.getValue()));
    }
    return ref;
  }



  /**
   * returns the {@link Map} of values.
   * 
   * @return the {@link Map} of values
   * @see com.genia.toolbox.web.jndi_config.StringContainer#getValues()
   */
  public Map<String, String> getValues()
  {
    return values;
  }



  /**
   * setter for the values property.
   * 
   * @param values
   *          the values to set
   */
  public void setValues(Map<String, String> values)
  {
    this.values = values;
  }

}
